/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.guide.service.MobileBusinessService;
import com.jeeplus.modules.guide.service.MobileMatterService;
import com.jeeplus.modules.guide.service.VersionService;
import com.jeeplus.modules.guide.utils.MatterUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
 * 事项Controller
 * @author azj
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/matter")
@RequiredArgsConstructor
public class MobileMatterController extends BaseController {
	private final MobileMatterService mobileMatterService;
	private final VersionService versionService;
	private final MatterUtils matterUtils;
	private final MobileBusinessService mobileBusinessService;
	/**
	 * getMatterValue 获取第三方事项集合并存库
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getMatterValue")
	public void getMatterValue() {
		String version = versionService.insertNowVersion();
		matterUtils.getMatterValue(version);
	}

	/**
	 * updateCacheVersion 更新固定版本
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="updateCacheVersion")
	public void updateCacheVersion(String version) {
		CacheUtils.put("version","version",version);
	}

	/**
	 * getInsertMatterValue 查询第三方事项集合入库结果比对
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getInsertMatterValue")
	public AjaxJson getInsertMatterValue() {
		return matterUtils.getInsertMatterValue(versionService.getNowCacheVersion());
	}



	/**
	 * getMatterAddress 获取第三方事项地址集合入库结果比对
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getMatterAddress")
	public void getMatterAddress() {
		matterUtils.getMatterAddress(versionService.getNowCacheVersion());
	}
	/**
	 * getMatterAddress 获取第三方事项地址集合结果查询
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getInsertMatterAddress")
	public AjaxJson getInsertMatterAddress() {
		return matterUtils.getInsertMatterAddress(versionService.getNowCacheVersion());
	}


	/**
	 * getSisutionValue 获取第三方事项情形化集合
	 * oid事项id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getSisutionValue")
	public AjaxJson getSisutionValue(String oid) {
		return AjaxJson.success("情形化加载成功！").put("data",matterUtils.getSisutionValue(oid));
	}

	/**
	 * insertNowVersion 获取新版本号
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="insertNowVersion")
	public AjaxJson insertNowVersion() {
		return AjaxJson.success("新版本加载成功！").put("version",versionService.insertNowVersion());
	}

	/**
	 * getMatterValue 获取事项集合展示查询
	 * organName组织名称
	 * matterName事项名称
	 * isTop是否热门事项
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getMatterList")
	public AjaxJson getMatterList(String organName,String matterName,int pageNo,int pageSize,String isTop) {
		Map<String,Object> map = new HashMap<>();
		map.put("curPage",pageNo);
		map.put("pageSize",pageSize);
		if(StringUtils.isNotBlank(matterName))
			matterName = "%"+matterName+"%";
		map.put("matterName",matterName);
		map.put("organName",organName);
		map.put("version",versionService.getVersion());
		map.put("isTop",isTop);
		return AjaxJson.success("事项加载成功！").put("data", mobileMatterService.getMatterList(map));
	}

	/**
	 * getMatterValue 获取热门事项配置
	 * oid事项id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getMatterTop")
	public AjaxJson getMatterTop(String oid) {
		return AjaxJson.success("事项加载成功！").put("data", mobileMatterService.getMatterTop(oid));
	}

	/**
	 * getMatterValue 获取热门事项配置
	 * oid事项id
	 * isTop是否热门事项
	 * isUpdate 是否修改 true是 false 否
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="editMatterTop")
	public AjaxJson editMatterTop(String oid,int sort,String isTop,String isUpdate) {
		mobileMatterService.editMatterTop(oid,sort,isTop,isUpdate);
		return AjaxJson.success("事项配置成功！");
	}

	/**
	 * editMatterType 配置业务事项映射
	 * typeId业务id
	 * ids事项id集合
	 * areaId点位标识
	 * type映射类型1是事项绑定2是一页一证绑定3是自助点位绑定
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="editMatterType")
	public AjaxJson editMatterType(String typeId,String ids,String areaId,int type) {
		String pid = mobileBusinessService.findPid(areaId);
		if(StringUtils.isBlank(pid)){
			pid = areaId;
		}
		mobileMatterService.editMatterType(typeId,ids,areaId,type,pid);
		return AjaxJson.success("业务事项配置成功！");
	}

	/**
	 * deleteMatter 删除配置业务事项映射
	 * typeId业务id
	 * ids事项id集合
	 * areaId点位标识
	 * type映射类型1是事项绑定2是一页一证绑定3是自助点位绑定
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="deleteMatter")
	public AjaxJson deleteMatter(String typeId,String ids,String areaId,int type) {
		mobileMatterService.deleteMatter(typeId,ids,areaId,type);
		return AjaxJson.success("业务事项配置成功！");
	}

	/**
	 * getBusinessDay 获取业务预约时间配置
	 * typeId业务id
	 * areaId点位标识
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getBusinessDay")
	public AjaxJson getBusinessDay(String typeId,String areaId) {
		return AjaxJson.success().put("data",mobileMatterService.getBusinessDay(typeId,areaId));
	}


	/**
	 * getBusinessMatter 获取业务事项绑定集合
	 * typeId业务id
	 * areaId点位标识
	 * type映射类型1是事项绑定2是一页一证绑定3是自助点位绑定
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getBusinessMatter")
	public AjaxJson getBusinessMatter(String typeId,String areaId,int type) {
		if(type == 3){
			matterUtils.insertOneThing();
		}
		return AjaxJson.success().put("data",mobileMatterService.getBusinessMatter(typeId,areaId,versionService.getVersion(),type));
	}

	/**
	 * insertMatterLabel 添加事项标签用于搜索查询
	 * oid事项id
	 * label 标签
	 * matterName事项名称
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="insertMatterLabel")
	public AjaxJson insertMatterLabel(String oid,String label,String matterName) {
		mobileMatterService.insertMatterLabel(UUID.randomUUID().toString(),oid,matterName,label);
		return AjaxJson.success("操作成功！");
	}

	/**
	 * insertMatterDay 添加业务预约时间人数配置
	 * isUpdate 是否修改 true是 false 否
	 * typeId业务id
	 * areaId点位标识
	 * dayDate时间段人数数组
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="insertMatterDay")
	public AjaxJson insertMatterDay(String isUpdate,String typeId,String areaId,@RequestParam("dayDate")String dayDate) {
		mobileMatterService.insertBusinessDay(isUpdate,typeId,areaId,dayDate);
		return AjaxJson.success("操作成功！");
	}


}
